﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace CRM.Database.Model
{
    public class EquipmentsCollection : AbstractCollection
    {
        /// <summary>
        /// Список оборудования
        /// </summary>
        public List<Equipments> EquipmentsList
        {
            get { return Entities.Equipments.ToList(); }
        }

        #region Implementation of IGetItem

        /// <summary>
        /// Получение типа текущего элемента
        /// </summary>
        /// <returns></returns>
        protected override Type GetItemType()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Получение статического списка для хранения объектов
        /// </summary>
        /// <returns></returns>
        public override object GetStaticCollection()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Получение элемента из коллекции по идентификатору
        /// </summary>
        /// <param name="itemId">Идентификатор</param>
        /// <returns>Элемент</returns>
        public override object GetItemById(int itemId)
        {
            return Entities.Equipments.Find(itemId);
        }

        /// <summary>
        /// Получение элемента из коллекции по идентификатору
        /// </summary>
        /// <param name="itemId">Идентификатор</param>
        /// <returns>Элемент</returns>
        public object GetItemByIdFromDatabase(int itemId)
        {
            return Entities.Equipments.FirstOrDefault(c => c.IdEquipment == itemId);
        }

        #endregion

        #region Implementation of IAddDeleteUpdate

        /// <summary>
        /// Обновление элемента в коллекции
        /// </summary>
        /// <param name="item">Новый элемент</param>
        /// <param name="userLogin">Логин пользователя, который делает изменения</param>
        /// <returns>Результат операции</returns>
        public bool Update(object item, string userLogin)
        {
            var newEquipment = (Equipments) item;
            if (newEquipment == null)
                return false;


            var Equipment4Update = GetItemByIdFromDatabase(newEquipment.IdEquipment) as Equipments;

            if (Equipment4Update == null)
                return true;

            if (!string.IsNullOrEmpty(newEquipment.Model))
                Equipment4Update.Model = newEquipment.Model;

            if (!string.IsNullOrEmpty(newEquipment.ProductionVersion))
                Equipment4Update.ProductionVersion = newEquipment.ProductionVersion;

            if (newEquipment.IdEquipmentType != null)
            {
                Equipment4Update.IdEquipmentType = newEquipment.IdEquipmentType;
            }

            if (newEquipment.IdVendor != null)
            {
                Equipment4Update.IdVendor = newEquipment.IdVendor;
            }

            if (newEquipment.ProductionDate != null)
            {
                Equipment4Update.ProductionDate = newEquipment.ProductionDate;
            }

            if (!string.IsNullOrEmpty(newEquipment.Comments))
                Equipment4Update.Comments = newEquipment.Comments;

            Equipment4Update.IsActive = newEquipment.IsActive;


            return base.Update(Equipment4Update, userLogin);
        }

        /// <summary>
        /// Обнуление статической коллекции
        /// </summary>
        protected override void SetStaticListToNULL()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region Implementation of IGetIdByName

        /// <summary>
        /// Получение списка элементов
        /// </summary>
        /// <returns>Список элементов</returns>
        public override List<object> GetList()
        {
            throw new NotImplementedException();
        }

        #endregion

        /// <summary>
        /// получения листа привязок оборудования к заявке по ИД заявки
        /// </summary>
        /// <param name="idRequest"></param>
        /// <returns></returns>
        public List<TechnicRequestsEquipments> GetBindingsEquipmentsListByRequestId(int idRequest)
        {
            var result = new List<TechnicRequestsEquipments>();
            var bindingCol = new TechnicRequestsEquipmentsCollection();
            if (bindingCol.TechnicRequestsEquipmentsList.Count < 1) return result;
            foreach (TechnicRequestsEquipments item in bindingCol.TechnicRequestsEquipmentsList)
            {
                if (item.IdTechnicRequest == idRequest)
                    result.Add(item);
            }
            return result;
        }

        /// <summary>
        /// получить список оборудования, прикрепленный к заявке
        /// </summary>
        /// <param name="idRequest"></param>
        /// <returns></returns>
        public List<Equipments> GetEquipmentsListByRequestId(int idRequest)
        {
            var result = new List<Equipments>();
            var requestsCol = new TechnicRequestsCollection();

            var request = (TechnicRequests) requestsCol.GetItemById(idRequest);
            if (request == null) return result;
            if (request.TechnicRequestsEquipments == null) return result;
            foreach (TechnicRequestsEquipments item in request.TechnicRequestsEquipments)
            {
                if (item.Equipments != null)
                    result.Add(item.Equipments);
            }
            return result;
        }
    }
}